package ru.spb.nicetu.schoolproject.gui;

import ru.spb.nicetu.schoolproject.data.SchoolClass;
import ru.spb.nicetu.schoolproject.data.Teacher;

import javax.swing.*;
import java.awt.*;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.text.NumberFormat;

/**
 * Created by dchernyshov on 11.04.15.
 */
public class ClassesCard extends CardTemplate<SchoolClass, Teacher> {
    private JFormattedTextField numClassField;
    private JCheckBox graduatedCheck;
    private JLabel graduatedYearLabel;
    private JFormattedTextField graduatedYearField;

    public ClassesCard() {
        super(Teacher.class, "Карточка класса", false, new SchoolClass());
        Box vbox = createVerticalBox();
        add(vbox, BorderLayout.CENTER);
    }

    public ClassesCard(SchoolClass schoolClass) {
        super(Teacher.class, "Карточка класса", true, schoolClass);
        Box vbox = createVerticalBox();
        add(vbox, BorderLayout.CENTER);
        numClassField.setValue(schoolClass.getClassNum());
        Teacher teacher = schoolClass.getFormMaster();
        comboBox.setSelectedItem(teacher);

    }

    @Override
    public void inputValue(SchoolClass object) {
        Number numClass = (Number)numClassField.getValue();
        object.setClassNum(numClass.intValue());
        Teacher teacher = (Teacher)comboBox.getSelectedItem();
        object.setFormMaster(teacher);
        if (graduatedCheck.isSelected()) {
            object.setGraduated(graduatedCheck.isSelected());
            Number year = (Number)graduatedYearField.getValue();
            object.setYear(year.intValue());
        }
    }

    @Override
    public boolean checkCard() {
        if (numClassField.getText().isEmpty()) {
            errorDialog("Введите номер класса!");
            return false;
        }
        if (graduatedCheck.isSelected() && graduatedYearField.getText().isEmpty()) {
            errorDialog("Введите год выпуска!");
            return false;
        }
        return true;
    }

    public Box createFirstHorizontalBox() {
        Box box = Box.createHorizontalBox();
        JLabel numClassLabel = new JLabel("№ класса:");
        box.add(numClassLabel);
        box.add(Box.createHorizontalStrut(10));
        numClassField = new JFormattedTextField(NumberFormat.getIntegerInstance());
        numClassField.setColumns(2);
        numClassField.setMaximumSize(
                numClassField.getPreferredSize());
        box.add(numClassField);
        return box;
    }

    public Box createThirdHorizontalBox() {
        Box box = Box.createHorizontalBox();
        final JLabel graduatedLabel = new JLabel("Выпустился?");
        box.add(graduatedLabel);
        box.add(Box.createHorizontalStrut(10));
        graduatedCheck = new JCheckBox();
        graduatedCheck.setSelected(false);
        graduatedCheck.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent actionEvent) {
                graduatedYearLabel.setEnabled(graduatedCheck.isSelected());
                graduatedYearField.setEnabled(graduatedCheck.isSelected());
            }
        });
        graduatedCheck.setMaximumSize(graduatedCheck.getPreferredSize());
        box.add(graduatedCheck);
        return box;
    }

    public Box createForthHorizontalBox() {
        Box box = Box.createHorizontalBox();
        graduatedYearLabel = new JLabel("Год выпуска");
        graduatedYearLabel.setEnabled(false);
        box.add(graduatedYearLabel);
        box.add(Box.createHorizontalStrut(10));
        graduatedYearField = new JFormattedTextField(NumberFormat.getIntegerInstance());
        graduatedYearField.setColumns(4);
        graduatedYearField.setMaximumSize(
                graduatedYearField.getPreferredSize());
        graduatedYearField.setEnabled(false);
        box.add(graduatedYearField);
        return box;
    }

    public Box createVerticalBox() {
        Box box = Box.createVerticalBox();
        Box hbox1 = createFirstHorizontalBox();
        box.add(hbox1);
        box.add(Box.createVerticalStrut(10));
        Box hbox2 = createSecondHorizontalBox("Кл. руководитель:");
        box.add(hbox2);
        box.add(Box.createHorizontalStrut(10));
        Box hbox3 = createThirdHorizontalBox();
        box.add(hbox3);
        Box hbox4 = createForthHorizontalBox();
        box.add(hbox4);
        box.add(Box.createHorizontalStrut(10));
        box.add(createLastHorizontalBox());
        box.add(Box.createVerticalGlue());
        return box;
    }
}
